package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.AdministratorLoginMapper;
import com.gtq.uhome.service.AdministratorLoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdministratorLoginServiceImpl implements AdministratorLoginService {
    @Resource
    AdministratorLoginMapper administratorLoginMapper;

    /**
     * 站点用户登录
     *
     * @param staffNumber   用户登录帐户
     * @param staffPassword 用户登录密码
     * @return true 登录成功 false 登录失败
     */
    @Override
    public Map<String, Object> administratorLogin(String staffNumber, String staffPassword) {
        //Integer integer = administratorLoginMapper.administratorLogin(staffNumber, staffPassword);
        //用户通过用户账号和密码登录，并查询权限
        List<String> userPower = administratorLoginMapper.administratorPower(staffNumber, staffPassword);
        //System.out.println("查询的权限"+strings);
        //用户通过用户账号和密码登录，查询角色
        String roleName = administratorLoginMapper.administratorRole(staffNumber, staffPassword);
        //System.out.println("角色名："+s);
        //查询用户ID
        Integer userId = administratorLoginMapper.administratorLogin(staffNumber, staffPassword);

        //查询结果是不是为空
        if (userPower.size()>0&&roleName!=null&&userId>0) {
            //生成一个map
            Map<String, Object> map = new HashMap<>();
            map.put("userId",userId);
            map.put("roleName", roleName);
            map.put("userPower", userPower);
            return map;
        }
        return null;
    }

    /**
     * 修改用户密码
     * @param staffNumber 用户账号
     * @param staffPassword 用户密码
     * @param newStaffPassword 用户新密码
     * @return true 修改成功 false 修改失败
     */
    @Override
    public Boolean updateAdministratorPassword(String staffNumber, String staffPassword, String newStaffPassword) {
        //查找当前用户ID
        Integer adminId = administratorLoginMapper.administratorLogin(staffNumber, staffPassword);
        if (adminId>0) {
            //修改用户密码
            Integer integer = administratorLoginMapper.updateAdministratorPassword(newStaffPassword, adminId);
            if (integer>0)
                return true;
        }
        return false;
    }

    /**
     * 修改派送员密码
     * @param staffId 派送员ID
     * @param staffPassword 派送员原密码
     * @param newStaffPassword 派送员新密码
     * @return true登录成功 false 登录失败
     */
    @Override
    public Boolean updateCourierAdministratorPassword(Integer staffId, String staffPassword, String newStaffPassword) {
        //调用修改方法
        Integer integer = administratorLoginMapper.updateCourierAdministratorPassword(staffId, staffPassword, newStaffPassword);
        //对返回结果判断
        if (integer>0)
            return true;
        return false;
    }


}
